{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #License Key Formatting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: licenseKeyFormatting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #密钥格式化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个许可密钥字符串 <code>s</code>，仅由字母、数字字符和破折号组成。字符串由 <code>n</code> 个破折号分成 <code>n + 1</code> 组。你也会得到一个整数 <code>k</code> 。</p>\n",
    "\n",
    "<p>我们想要重新格式化字符串&nbsp;<code>s</code>，使每一组包含 <code>k</code> 个字符，除了第一组，它可以比 <code>k</code> 短，但仍然必须包含至少一个字符。此外，两组之间必须插入破折号，并且应该将所有小写字母转换为大写字母。</p>\n",
    "\n",
    "<p>返回 <em>重新格式化的许可密钥</em> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>S = \"5F3Z-2e-9-w\", k = 4\n",
    "<strong>输出：</strong>\"5F3Z-2E9W\"\n",
    "<strong>解释：</strong>字符串 S 被分成了两个部分，每部分 4 个字符；\n",
    "&nbsp;    注意，两个额外的破折号需要删掉。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>S = \"2-5g-3-J\", k = 2\n",
    "<strong>输出：</strong>\"2-5G-3J\"\n",
    "<strong>解释：</strong>字符串 S 被分成了 3 个部分，按照前面的规则描述，第一部分的字符可以少于给定的数量，其余部分皆为 2 个字符。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示:</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>s</code>&nbsp;只包含字母、数字和破折号&nbsp;<code>'-'</code>.</li>\n",
    "\t<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [license-key-formatting](https://leetcode.cn/problems/license-key-formatting/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [license-key-formatting](https://leetcode.cn/problems/license-key-formatting/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"5F3Z-2e-9-w\"\\n4', '\"2-5g-3-J\"\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = s.replace(\"-\",'').upper()[::-1]\n",
    "\n",
    "        if not s:\n",
    "            return ''\n",
    "\n",
    "        total = len(s)\n",
    "        ans = ''       \n",
    "\n",
    "        for i in range(0, total//k):\n",
    "            ans += s[i*k:(i+1)*k] + '-'\n",
    "        if total%k==0:\n",
    "            ans = ans[:-1]\n",
    "        return (ans + s[(total//k)*k:])[::-1]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        i = len(s) - 1\n",
    "        tmp = \"\"\n",
    "        re = \"\"\n",
    "        while i >= 0:\n",
    "            if s[i] != \"-\" and len(tmp) < k:\n",
    "                tmp = s[i].upper() + tmp\n",
    "            if len(tmp) == k:\n",
    "                if re == \"\":\n",
    "                    re += tmp\n",
    "                else:\n",
    "                    re = tmp + \"-\" + re\n",
    "                tmp = \"\"\n",
    "            i -= 1\n",
    "        if tmp != \"\" :\n",
    "            if re != \"\":\n",
    "                re = tmp + \"-\" + re\n",
    "            else:\n",
    "                re = tmp\n",
    "        return re\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = s.replace('-', '').upper()\n",
    "        n = len(s)\n",
    "        first_cnt =  n % k\n",
    "        ans = s[0: first_cnt]\n",
    "        for i in range(first_cnt, n, k):\n",
    "            if i != 0:\n",
    "                ans += '-'\n",
    "            ans += s[i: i + k]\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s_ = s\n",
    "        s = ''\n",
    "        count = 0\n",
    "        for i in s_[::-1]:\n",
    "            if i == '-':\n",
    "                continue\n",
    "            if 'a' <= i <= 'z':\n",
    "                s = i.upper() + s\n",
    "            else:\n",
    "                s = i + s\n",
    "            count += 1\n",
    "            if count == k:\n",
    "                s = '-' + s\n",
    "                count = 0\n",
    "        if s == '':\n",
    "            return s\n",
    "        if s[0] == '-':\n",
    "            s = s[1:]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ans=\"\"\n",
    "        count=0\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            if s[i]!=\"-\":\n",
    "                ans=s[i].upper()+ans\n",
    "                count+=1\n",
    "            if count==k:\n",
    "                ans=\"-\"+ans\n",
    "                count=0\n",
    "        if ans:\n",
    "            if ans[0]==\"-\":\n",
    "                ans=ans[1:]\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s_ = s\n",
    "        s = ''\n",
    "        count = 0\n",
    "        for i in range(len(s_) - 1, -1 ,-1):\n",
    "            if  s_[i] == '-':\n",
    "                continue\n",
    "            if 'a' <= s_[i] <= 'z':\n",
    "                s = s_[i].upper() + s\n",
    "            else:\n",
    "                s = s_[i] + s\n",
    "            count += 1\n",
    "            if count == k:\n",
    "                s = '-' + s\n",
    "                count = 0\n",
    "        if s == '':\n",
    "            return s\n",
    "        if s[0] == '-':\n",
    "            s = s[1:]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = s.replace('-', '').upper()\n",
    "        if not s:\n",
    "            return ''\n",
    "        l = len(s)\n",
    "        s = s[::-1]\n",
    "        r = ''\n",
    "        for i in range(l // k):\n",
    "            r += s[i * k: i * k + k] + '-'\n",
    "        r += s[l - (l % k): l]\n",
    "        r = r[::-1]\n",
    "        if r[0] == '-':\n",
    "            r = r[1:]\n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s1=s.upper().replace('-','')[::-1]\n",
    "        m=''\n",
    "        while len(s1)>k:\n",
    "            m=m+s1[0:k]+'-'\n",
    "            s1=s1[k:len(s1)]\n",
    "        else:\n",
    "            m+=s1\n",
    "        return m[::-1]\n",
    "\n",
    "        \n",
    "            \n",
    "        \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s_ = s\n",
    "        s = ''\n",
    "        count = 0\n",
    "        for i in s_[::-1]:\n",
    "            if i == '-':\n",
    "                continue\n",
    "            if 'a' <= i <= 'z':\n",
    "                s = i.upper() + s\n",
    "            else:\n",
    "                s = i + s\n",
    "            count += 1\n",
    "            if count == k:\n",
    "                s = '-' + s\n",
    "                count = 0\n",
    "        if s == '':\n",
    "            return s\n",
    "        if s[0] == '-':\n",
    "            s = s[1:]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = s.replace(\"-\", \"\").upper()\n",
    "        n = len(s)\n",
    "\n",
    "        def fun(s):\n",
    "            j = 0\n",
    "            l = []\n",
    "            for i in range(k, n + 1, k):\n",
    "                l.append(s[j:i])\n",
    "                j += k\n",
    "            return \"-\".join(l)\n",
    "\n",
    "\n",
    "        if n % k == 0:\n",
    "            return fun(s)\n",
    "        else:\n",
    "            for i in range(len(s)):\n",
    "                if len(s[i:]) % k == 0:\n",
    "                    return s[:i] + \"-\" + fun(s[i:])\n",
    "            else:\n",
    "                return s.upper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s=s.upper()\n",
    "        s=s.replace(\"-\",\"\")\n",
    "        ans=s[:len(s)%k]\n",
    "        if len(s)%k>0 and len(s)%k!=len(s):\n",
    "            ans+=\"-\"\n",
    "        cnt=0\n",
    "        for i in range(len(s)%k,len(s)):\n",
    "            if cnt==k:\n",
    "                ans+=\"-\"\n",
    "                cnt=0\n",
    "            ans+=s[i]\n",
    "            cnt+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = s.replace('-', '').upper()\n",
    "        if len(s) % k:\n",
    "            ans = [s[:len(s) % k]]\n",
    "        else:\n",
    "            ans = []\n",
    "        for i in range(len(s) % k, len(s), k):\n",
    "            ans.append(s[i: i + k])\n",
    "        return '-'.join(ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, S: str, K: int) -> str:\n",
    "        rawS = S.replace('-','').upper()\n",
    "        len_rawS = len(rawS)\n",
    "        if K >= len_rawS:\n",
    "            return rawS\n",
    "        extra = len_rawS % K\n",
    "        seg = len_rawS // K\n",
    "        finalS = '-'.join([rawS[i*K+extra:(i+1)*K+extra] for i in range(seg)])\n",
    "        if extra > 0:\n",
    "            finalS = rawS[:extra] + '-' + finalS\n",
    "        return finalS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        # s = s.replace('-', '').upper()       # 逆序存\n",
    "        # if not s:\n",
    "        #     return ''\n",
    "        # l = len(s)\n",
    "        # s = s[::-1]\n",
    "        # r = ''\n",
    "        # for i in range(l // k):\n",
    "        #     r += s[i * k: i * k + k] + '-'        # 关键语法\n",
    "        # r += s[l - (l % k): l]\n",
    "        # r = r[::-1]\n",
    "        # if r[0] == '-':\n",
    "        #     r = r[1:]\n",
    "        # return r\n",
    "        s =  s.replace('-', '').upper()\n",
    "        l = len(s)\n",
    "        i = 0\n",
    "        ll = []\n",
    "        while l:\n",
    "            c = l % k if l % k else k   #正序取，先取余数放第一个，其他正好整除，取k个\n",
    "            ll.append(s[i:i + c])\n",
    "            i += c\n",
    "            l -= c\n",
    "        return '-'.join(ll)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s_list = s.split('-')\n",
    "        s = ''.join(s_list)\n",
    "        # s = s.replace('-', '')\n",
    "        n = len(s)\n",
    "        n_start = n % k\n",
    "        ans = s[:n_start].upper()\n",
    "        for i in range(n // k):\n",
    "            if not ans:\n",
    "                ans += s[n_start + i * k : n_start + (i+1) * k].upper()\n",
    "            else:\n",
    "                ans += '-' + s[n_start + i * k : n_start + (i+1) * k].upper()\n",
    "\n",
    "        # if n_start == 0:\n",
    "        #     ans = ans[1:]\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        new_arr = s.upper().split('-')\n",
    "        temp = ''\n",
    "        for i in range(len(new_arr)):\n",
    "            temp += new_arr[i]\n",
    "        first = len(temp) % k\n",
    "        ans = ''\n",
    "        ans += temp[:first] + '-'\n",
    "        trans = ''\n",
    "        for i in range(first,len(temp)):\n",
    "            trans += temp[i]\n",
    "            if len(trans) == k:\n",
    "                ans += trans\n",
    "                ans += '-'\n",
    "                trans = ''\n",
    "        return ans.strip('-')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        new = \"\".join(s.split(\"-\")).upper()\n",
    "        ret = []\n",
    "        for i in range(len(new),0,-k):\n",
    "            ret.insert(0,new[max(0,i-k):i])\n",
    "        return \"-\".join(ret)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        l = s.split('-')\n",
    "        ss = ''\n",
    "        for i in range(len(l)):\n",
    "            ss += l[i]\n",
    "        \n",
    "        n = len(ss)\n",
    "        num = n//k \n",
    "        first = n%k \n",
    "        if num == 0:\n",
    "            return ss.upper()\n",
    "        else:\n",
    "            if first == 0:\n",
    "                ans = ''\n",
    "                for i in range(0,n,k):\n",
    "                    ans += ss[i:i+k]\n",
    "                    ans +='-'\n",
    "                return ans[:-1].upper()\n",
    "            else:\n",
    "                ans = ss[0:first]\n",
    "                ans += '-'\n",
    "                for i in range(first,n,k):\n",
    "                    ans += ss[i:i+k]\n",
    "                    ans += '-'\n",
    "                return ans[:-1].upper()\n",
    "\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s_len = self.getSLength(s)\n",
    "        first_len = 0\n",
    "        for i in range(1, k + 1):\n",
    "            if (s_len - i) % k == 0:\n",
    "                first_len = i\n",
    "                break\n",
    "        s_list = s.split('-')\n",
    "        new_s_list = []\n",
    "        for ss in s_list:\n",
    "            new_s_list.append(ss.upper())\n",
    "        new_s = \"\".join(new_s_list)\n",
    "        ret = new_s[:first_len] + \"-\"\n",
    "        for i in range(first_len, s_len, k):\n",
    "            ret += new_s[i: i + k] + '-'\n",
    "        return ret[:-1]\n",
    "\n",
    "    def getSLength(self, s: str) -> int:\n",
    "        s_list = s.split('-')\n",
    "        length = 0\n",
    "        for ss in s_list:\n",
    "            length += len(ss)\n",
    "        return length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s_len = self.getSLength(s)\n",
    "        # print(s_len)\n",
    "        first_len = 0\n",
    "        for i in range(1, k + 1):\n",
    "            if (s_len - i) % k == 0:\n",
    "                first_len = i\n",
    "                break\n",
    "        s_list = s.split('-')\n",
    "        new_s_list = []\n",
    "        for ss in s_list:\n",
    "            new_s_list.append(ss.upper())\n",
    "        new_s = \"\".join(new_s_list)\n",
    "        print(new_s)\n",
    "        ret = new_s[:first_len] + \"-\"\n",
    "        for i in range(first_len, s_len, k):\n",
    "            ret += new_s[i: i + k] + '-'\n",
    "            # if i + k < s_len - 1:\n",
    "            #     ret += new_s[i: i + k] + '-'\n",
    "            # else:\n",
    "            #     ret += new_s[i: i + k]\n",
    "        # print(ret)\n",
    "        return ret[:-1]\n",
    "\n",
    "    def getSLength(self, s: str) -> int:\n",
    "        s_list = s.split('-')\n",
    "        length = 0\n",
    "        for ss in s_list:\n",
    "            length += len(ss)\n",
    "        return length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        index_list=[]\n",
    "        s1=''.join(s.split('-'))\n",
    "        s2=list(s1.upper())\n",
    "        i=len(s2)-1\n",
    "        while(i>0):\n",
    "            if(i%k==0):\n",
    "                s2.insert(-i,'-')\n",
    "            i-=1\n",
    "        return ''.join(s2) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        arr=s.split(\"-\")\n",
    "\n",
    "\n",
    "        news=\"\".join(arr).upper()\n",
    "\n",
    "        ans=[]\n",
    "      #  ans.append(arr[0])\n",
    "        print(news)\n",
    "        for i in range(len(news)-1,-1,-k):\n",
    "            ans.append(news[max(i-k+1,0):i+1])\n",
    "        \n",
    "        ans=ans[::-1]\n",
    "        return \"-\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\r\n",
    "        t = \"\".join(s.split(\"-\"))\r\n",
    "        n = len(t)\r\n",
    "        f = \"\"\r\n",
    "        for i in range(n):\r\n",
    "            if t[i].isalpha():\r\n",
    "                f += t[i].upper()\r\n",
    "            else:\r\n",
    "                f += t[i]\r\n",
    "        res = []\r\n",
    "        r = n % k\r\n",
    "        if r == 0:\r\n",
    "            for i in range(0, n, k):\r\n",
    "                res.append(f[i:i+k])\r\n",
    "        else:\r\n",
    "            res.append(f[0:r])\r\n",
    "            for i in range(r, n, k):\r\n",
    "                res.append(f[i:i+k])\r\n",
    "        return \"-\".join(res)\r\n",
    "        \r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s=s.upper()\n",
    "        s=s.split(\"-\")\n",
    "        s=\"\".join(i for i in s)\n",
    "        n=len(s)\n",
    "        ss=[]\n",
    "        for i in range(n%k,n,k):\n",
    "            ss.append(s[i:i+k])\n",
    "        ss.insert(0,s[:n%k])\n",
    "        result=\"-\".join(i for i in ss)\n",
    "        result=result.strip(\"-\")\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s=s.upper()\n",
    "        n=len(s)\n",
    "        a=[]\n",
    "        c=0\n",
    "        for i in range(n-1,-1,-1):\n",
    "            if s[i]!=\"-\":\n",
    "                a.append(s[i])\n",
    "                c+=1\n",
    "                if c%k==0:\n",
    "                    a.append('-')\n",
    "        if a and a[-1]==\"-\":\n",
    "            a.pop()\n",
    "        return ''.join(a[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        # 思路： 将字符去除破折号后进行翻转，每隔k个进行切片\n",
    "        s = s[::-1].replace(\"-\",\"\")\n",
    "        # w9e2z3f5\n",
    "        count = 0\n",
    "        ans = []\n",
    "        for i in s:\n",
    "            count +=1\n",
    "            ans.append(i)\n",
    "            if count %k == 0 and count!=len(s):\n",
    "                ans.append(\"-\")\n",
    "        \n",
    "        return \"\".join(ans[::-1]).upper()\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        l=list()\n",
    "        cnt=0\n",
    "        s=s.upper()\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            if s[i]!='-':\n",
    "                l.append(s[i])\n",
    "                cnt+=1\n",
    "                if cnt % k==0 and i>0:\n",
    "                  l.append('-')\n",
    "        if l and l[-1]=='-':\n",
    "            l.pop()\n",
    "                \n",
    "        return ''.join(l[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        l=list()\n",
    "        cnt=0\n",
    "        s=s.replace('-','')\n",
    "        s=s.upper()\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            if s[i]!='-':\n",
    "                l.append(s[i])\n",
    "                cnt+=1\n",
    "                if cnt % k==0 and i>0:\n",
    "                  l.append('-')\n",
    "                \n",
    "        return ''.join(l[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        l=list()\n",
    "        cnt=0\n",
    "        s=s.upper()\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            if s[i]!='-':\n",
    "                l.append(s[i])\n",
    "                cnt+=1\n",
    "                if cnt % k==0 and i>0:\n",
    "                  l.append('-')\n",
    "        if l and l[-1]=='-':\n",
    "            l.pop()\n",
    "                \n",
    "        return ''.join(l[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        snew = []\n",
    "        for si in s:\n",
    "            if si != '-':\n",
    "                if not si.isdigit():\n",
    "                    snew.append(si.upper())\n",
    "                else:\n",
    "                    snew.append(si)\n",
    "        \n",
    "        # print(snew)\n",
    "        sout = ''\n",
    "        cut = len(snew) % k\n",
    "        for j in range(len(snew)):\n",
    "            sout += snew[j]\n",
    "            if (j+1 == cut or (j+1-cut)%k == 0) and j+1 != len(snew):\n",
    "                sout += '-'\n",
    "        \n",
    "        # print(sout)\n",
    "        return sout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s1=s.upper().replace('-','')[::-1]\n",
    "        m=[]\n",
    "        a=0\n",
    "        for i in range(0,len(s1)):\n",
    "            m.append(s1[i])\n",
    "            a+=1\n",
    "            if a%k==0:\n",
    "                m.append('-')\n",
    "        if m and m[-1]=='-':\n",
    "            m.pop()\n",
    "        return ''.join(m[::-1])\n",
    "\n",
    "            \n",
    "\n",
    "        \n",
    "            \n",
    "        \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ans=[]\n",
    "        cnt=0\n",
    "        for i in s[::-1]:\n",
    "            if i!='-':\n",
    "                if cnt<k:\n",
    "                    ans.append(i)\n",
    "                    cnt+=1\n",
    "                else:\n",
    "                    ans.append('-')\n",
    "                    ans.append(i)\n",
    "                    cnt=1\n",
    "        return ''.join(ans[::-1]).upper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ans = []\n",
    "        c = 0\n",
    "        for i in s[::-1]:\n",
    "            if i == '-':\n",
    "                continue\n",
    "            c += 1\n",
    "            ans.append(i)\n",
    "            if not c % k:\n",
    "                ans.append('-')\n",
    "        if ans and ans[-1] == '-':\n",
    "            ans.pop()\n",
    "        return ''.join(ans[::-1]).upper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s=''.join(s.split('-')).upper()\n",
    "        i=len(s)-1\n",
    "        j=0\n",
    "        res=[]\n",
    "        while i>=0:\n",
    "            if j==k:\n",
    "                res.append('-')\n",
    "                j=0\n",
    "            else:\n",
    "                res.append(s[i])\n",
    "                i-=1\n",
    "                j+=1\n",
    "        return ''.join(res[::-1])\n",
    "        \n",
    "                \n",
    "                \n",
    "            \n",
    "            \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = s.upper()\n",
    "        lista = []\n",
    "        for i in s:\n",
    "            if i != '-':\n",
    "                lista.append(i)\n",
    "        if len(lista) == 0:\n",
    "            return ''\n",
    "        a = len(lista) % k\n",
    "        for i in range(len(lista) // k):\n",
    "            lista[i * k + a - 1] = lista[i * k + a - 1] + '-'\n",
    "        if a == 0:\n",
    "            lista[-1] = lista[-1][0]\n",
    "        str = ''\n",
    "        for i in lista:\n",
    "            str += i\n",
    "        return str\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s=s.upper().replace(\"-\",\"\")\n",
    "        if len(s)==1:\n",
    "            return s\n",
    "        b,c=\"\",[]\n",
    "        if len(s)%k!=0:\n",
    "            c.append(s[0:len(s)%k]+\"-\")\n",
    "            s=s[len(s)%k:]\n",
    "        for i,j in enumerate(s):\n",
    "            b+=j\n",
    "            if len(b)==k:\n",
    "                if i==len(s)-1:\n",
    "                    c.append(b)\n",
    "                else:\n",
    "                    c.append(b+\"-\")\n",
    "                    b=\"\"\n",
    "        return \"\".join(c)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        re = ''\n",
    "        s = \"\".join(s.split(\"-\")).upper()\n",
    "        ss = list(s)\n",
    "        l = k\n",
    "        while k < len(ss):\n",
    "            ss[-k-1] += \"-\"\n",
    "            k += l\n",
    "        return ''.join(ss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        result = []\n",
    "        counter =0\n",
    "        tmp = ''\n",
    "        for i in range(len(s)-1, -1,-1):\n",
    "            if s[i]=='-':\n",
    "                continue\n",
    "            tmp =s[i].upper()+tmp\n",
    "            counter+=1\n",
    "            if counter%k==0:\n",
    "                result.append(tmp)\n",
    "                tmp =''\n",
    "        if tmp:\n",
    "            result.append(tmp)\n",
    "        return '-'.join(result[::-1])\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        res = list()\n",
    "        count = 0\n",
    "        s = list(s)[::-1]\n",
    "        for v in s:\n",
    "            if v == \"-\":\n",
    "                continue\n",
    "            if count < k:\n",
    "                count += 1\n",
    "                res += v.upper()\n",
    "            else:\n",
    "                count = 0\n",
    "                res += \"-\"\n",
    "                res += v.upper()\n",
    "                count += 1\n",
    "        res = res[::-1]\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        #ascii a~z:97~122 A~Z:65~90 数字:48~57\n",
    "        s=list(s)\n",
    "        count=0\n",
    "        res=[]\n",
    "        i=len(s)-1\n",
    "\n",
    "        #从后往前循环\n",
    "        while i>=0:\n",
    "            #小写转大写\n",
    "            if ord(s[i])>=97 and ord(s[i])<=122:\n",
    "                res.append(chr(ord(s[i])-32))\n",
    "                count+=1\n",
    "            elif (ord(s[i])>=48 and ord(s[i])<=57) or (ord(s[i])>=65 and ord(s[i])<=90):\n",
    "                res.append(s[i])\n",
    "                count+=1\n",
    "            if count==k and i!=0:\n",
    "                res.append('-')\n",
    "                count=0\n",
    "            i-=1\n",
    "        if res!=[] and res[-1]=='-':\n",
    "            res=res[0:-1]\n",
    "        return ''.join(res[::-1])\n",
    "\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        list_key = [i.upper() for i in s if i != \"-\"]\n",
    "        result = \"\"\n",
    "        num = 0\n",
    "        for i in range(len(list_key) - 1, -1, -1):\n",
    "            result = f\"{list_key[i]}\" + result\n",
    "            num += 1\n",
    "            if num % k == 0 and i != 0:\n",
    "                result = \"-\" + result\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        result = []\n",
    "        temp = \"\"\n",
    "        for i in s[::-1]:\n",
    "            if len(temp) < k and i != \"-\":\n",
    "                temp += i\n",
    "            elif len(temp) == k and i != \"-\":\n",
    "                result.append(temp[::-1].upper())\n",
    "                temp = i\n",
    "        if temp != \"\":\n",
    "            result.append(temp[::-1].upper())\n",
    "        return \"-\".join(result[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        lis = []\n",
    "        for num in s:\n",
    "            if num != '-':\n",
    "                lis.append(num.upper())\n",
    "        fir = len(lis) % k\n",
    "        res = ''\n",
    "        for i in range(len(lis)):\n",
    "            if i == fir and i != 0:\n",
    "                res += '-' \n",
    "            if (i - fir) % k == 0 and i >= k:\n",
    "                res += '-'\n",
    "            res += lis[i]\n",
    "        return res\n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = list(s.upper())\n",
    "        s=s[::-1]\n",
    "\n",
    "        ss=[]\n",
    "        cnt = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i]==\"-\":\n",
    "                continue\n",
    "            if cnt == k:\n",
    "                    ss.append(\"-\")\n",
    "                    cnt =0\n",
    "            if cnt < k:\n",
    "                ss.append(s[i])\n",
    "                cnt+=1\n",
    "                \n",
    "        s = \"\".join(ss[::-1])\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        num=0\n",
    "        for i in s:\n",
    "            if i=='-':\n",
    "                num+=1\n",
    "        cnt=len(s)-num\n",
    "        flag=0\n",
    "        if cnt%k>0:\n",
    "            m=cnt//k\n",
    "            n=cnt%k\n",
    "            flag=1\n",
    "        else:\n",
    "            m=cnt//k\n",
    "            n=0\n",
    "        i=0\n",
    "        res=[]\n",
    "        while i<len(s):\n",
    "            while n and i<len(s):\n",
    "                if s[i]=='-':\n",
    "                    i+=1\n",
    "                    continue\n",
    "                if s[i]>='a' and s[i]<='z':\n",
    "                    res.append(s[i].upper())\n",
    "                    i+=1\n",
    "                else:\n",
    "                    res.append(s[i])\n",
    "                    i+=1\n",
    "                n-=1\n",
    "            if flag==1 and i<len(s):\n",
    "                res.append('-')\n",
    "                flag=0\n",
    "            t=k\n",
    "            while t and i<len(s):\n",
    "                if s[i]=='-':\n",
    "                    i+=1\n",
    "                    continue\n",
    "                if s[i]>='a' and s[i]<='z':\n",
    "                    res.append(s[i].upper())\n",
    "                    i+=1\n",
    "                else:\n",
    "                    res.append(s[i])\n",
    "                    i+=1\n",
    "                t-=1\n",
    "            m-=1\n",
    "            if m>0:\n",
    "                res.append('-')\n",
    "        return ('').join(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s=list((''.join(s.split('-'))))\n",
    "        s=[c.upper() if c.islower() else c for c in s]\n",
    "        # print(s)\n",
    "        n=len(s)\n",
    "        res=[]\n",
    "        if n%k!=0:\n",
    "            res.append(''.join(s[:n%k])) \n",
    "        # print(s[:n%k])\n",
    "        for i in range(n%k,n,k):\n",
    "            res.append(''.join(s[i:i+k]))\n",
    "        return '-'.join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        curGroupNum = 0\n",
    "        list1 = []\n",
    "        for index1 in range(len(s)-1, -1, -1):\n",
    "            if s[index1].isalnum():\n",
    "                if curGroupNum == k:\n",
    "                    curGroupNum = 0\n",
    "                    list1.insert(0, '-')\n",
    "\n",
    "                curGroupNum += 1\n",
    "                list1.insert(0, s[index1].upper())\n",
    "\n",
    "        return ''.join(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\r\n",
    "        s = \"\".join(s.split(\"-\")).upper()\r\n",
    "        #print(s)\r\n",
    "        res = []\r\n",
    "        #print(len(s))\r\n",
    "        for i in range(len(s)//k):\r\n",
    "            res.append(s[::-1][i*k:i*k+k][::-1])\r\n",
    "        if len(s)%k != 0:\r\n",
    "            \r\n",
    "            res.append(s[:len(s)%k])\r\n",
    "        return '-'.join(res[::-1])\r\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        res = []\n",
    "        for i in s:\n",
    "            if i != '-':\n",
    "                res.append(i.upper())\n",
    "        l=len(res)\n",
    "        if l%k==0:\n",
    "            for i in range(1,l//k):\n",
    "                res.insert(k*i+i-1,'-')\n",
    "        else:\n",
    "            for i in range(l//k):\n",
    "                res.insert(l%k+k*i+i,'-')\n",
    "        a=''.join(res)\n",
    "        return a\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = list(\"\".join(s.split(\"-\")))\n",
    "        print(\"s=\",s)\n",
    "        n = 0\n",
    "        list1 = []\n",
    "        for i in range(-k,-len(s),-k):\n",
    "            list1.append(i)\n",
    "            print(i)\n",
    "        for i in list1:\n",
    "            s.insert(i-n,\"-\")\n",
    "            n += 1\n",
    "        s = \"\".join(s).upper()\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        li:list[char]=[]\n",
    "        pos=0\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            ch=s[i]\n",
    "            if ch!='-':\n",
    "                pos+=1\n",
    "                if ch>='a' and ch<='z':\n",
    "                    ch=ch.upper()\n",
    "                li.append(ch)\n",
    "                if pos%k==0:\n",
    "                    li.append('-')\n",
    "        if len(li)>0 and li[len(li)-1]=='-':\n",
    "            li=li[:-1]\n",
    "        li.reverse()\n",
    "        return \"\".join(li)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ans = list()\n",
    "        cnt = 0\n",
    "\n",
    "        for i in range(len(s) - 1, -1, -1):\n",
    "            if s[i] != \"-\":\n",
    "                ans.append(s[i].upper())\n",
    "                cnt += 1\n",
    "                if cnt % k == 0:\n",
    "                    ans.append(\"-\")\n",
    "        \n",
    "        if ans and ans[-1] == \"-\":\n",
    "            ans.pop()\n",
    "        \n",
    "        return \"\".join(ans[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        res = []\n",
    "        for ch in s:\n",
    "            if ch != '-':\n",
    "                if ch.isalpha() and ch.islower():\n",
    "                        res.append(ch.upper())\n",
    "                else:\n",
    "                    res.append(ch)\n",
    "        i = len(res) - 1\n",
    "        ans = []\n",
    "        while i >= k:\n",
    "            ans.append(''.join(res[i-k+1:i+1]))\n",
    "            i -= k\n",
    "        print(ans)\n",
    "        ans.append(''.join(res[:i+1]))\n",
    "        print(ans)\n",
    "        return '-'.join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\r\n",
    "        ans = list()\r\n",
    "        cnt = 0\r\n",
    "\r\n",
    "        for i in range(len(s) - 1, -1, -1):\r\n",
    "            if s[i] != \"-\":\r\n",
    "                ans.append(s[i].upper())\r\n",
    "                cnt += 1\r\n",
    "                if cnt % k == 0:\r\n",
    "                    ans.append(\"-\")\r\n",
    "        \r\n",
    "        if ans and ans[-1] == \"-\":\r\n",
    "            ans.pop()\r\n",
    "        \r\n",
    "        return \"\".join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ss = list()\n",
    "        num = 0\n",
    "        i = len(s) - 1\n",
    "        while i >= 0:\n",
    "            if s[i] != \"-\":\n",
    "                if s[i].islower():\n",
    "                    l = s[i].upper()\n",
    "                    # print(l)\n",
    "                    ss.append(l)\n",
    "                    num += 1\n",
    "                else:\n",
    "                    ss.append(s[i])\n",
    "                    num += 1\n",
    "                if num == k:\n",
    "                    ss.append(\"-\")\n",
    "                    num = 0\n",
    "            i -= 1\n",
    "        # ss = ss[::-1]\n",
    "        if ss:\n",
    "            if ss[-1] == \"-\":\n",
    "                ss.pop()\n",
    "\n",
    "\n",
    "            return \"\".join(ss[::-1])\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        res = []\n",
    "        split_string = s.split(\"-\")\n",
    "        split_string = \"\".join(split_string)\n",
    "        if not split_string:\n",
    "            return \"\"\n",
    "\n",
    "        if len(split_string) % k == 0:\n",
    "            split_string = \"\".join(split_string)\n",
    "            word = \"\"\n",
    "            for i in range(len(split_string)):\n",
    "                word += split_string[i].upper()\n",
    "                if (i + 1) % k == 0:\n",
    "                    res.append(word)\n",
    "                    word = \"\"\n",
    "   \n",
    "        else:\n",
    "            mod = len(split_string) % k\n",
    "            res.append(split_string[:mod].upper())\n",
    "            split_string = split_string[mod:]\n",
    "            word = \"\"\n",
    "            for i in range(len(split_string)):\n",
    "                word += split_string[i].upper()\n",
    "                if (i + 1) % k == 0:\n",
    "                    res.append(word)\n",
    "                    word = \"\"\n",
    "\n",
    "        return \"-\".join(res)\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        cnt = 0\n",
    "        for c in s:\n",
    "            if c != '-':\n",
    "                cnt += 1\n",
    "        m = cnt%k\n",
    "        if m == 0:\n",
    "            m = k\n",
    "        ansList = []\n",
    "        ans = ''\n",
    "        for c in s:\n",
    "            if c!= '-':\n",
    "                if len(ansList) == 0 and len(ans) < m:\n",
    "                    ans+=c\n",
    "                elif len(ansList) == 0 and len(ans) == m:\n",
    "                    ansList.append(ans.upper())\n",
    "                    ans = c\n",
    "                elif len(ans) < k:\n",
    "                    ans+=c\n",
    "                elif len(ans) == k:\n",
    "                    ansList.append(ans.upper())\n",
    "                    ans = c\n",
    "        ansList.append(ans.upper())\n",
    "\n",
    "        return '-'.join(ansList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        # res = []\n",
    "        # cnt = 0\n",
    "        # for i in range(len(s) - 1, -1, -1):\n",
    "        #     if s[i] != '-':\n",
    "        #         res.append(s[i].upper())\n",
    "        #         cnt += 1\n",
    "        #         if cnt % k == 0:\n",
    "        #             res.append('-')\n",
    "        # if res and res[-1] == '-':\n",
    "        #     res.pop()\n",
    "        # return ''.join(res[::-1])\n",
    "\n",
    "\n",
    "        s = ''.join(s.split('-'))[::-1]\n",
    "        res = []\n",
    "        for i in range(0, len(s), k):\n",
    "            res.append(s[i: i+k].upper())\n",
    "        return '-'.join(res)[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = ''.join(s.split(\"-\"))[::-1]\n",
    "        res = []\n",
    "        for i in range(0, len(s), k):\n",
    "            res.append(s[i:i + k].upper())\n",
    "        return \"-\".join(res)[::-1]\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        # res = []\n",
    "        # cnt = 0\n",
    "        # for i in range(len(s) - 1, -1, -1):\n",
    "        #     if s[i] != '-':\n",
    "        #         res.append(s[i].upper())\n",
    "        #         cnt += 1\n",
    "        #         if cnt % k == 0:\n",
    "        #             res.append('-')\n",
    "        # if res and res[-1] == '-':\n",
    "        #     res.pop()\n",
    "        # return ''.join(res[::-1])\n",
    "\n",
    "\n",
    "        s = ''.join(s.split('-'))[::-1]\n",
    "        res = []\n",
    "        for i in range(0, len(s), k):\n",
    "            res.append(s[i: i+k].upper())\n",
    "        return '-'.join(res)[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        new_s = \"\".join([c.upper() for c in s if c != \"-\"])\n",
    "        count = sum(Counter(new_s).values())\n",
    "        first_number = count % k\n",
    "        ll = []\n",
    "        m = 0\n",
    "        if first_number != 0:\n",
    "            ll.append(new_s[:first_number])\n",
    "            m += 1\n",
    "        for i in range(first_number, len(new_s), k):\n",
    "            ll.append(new_s[i:i + k])\n",
    "            m += 1\n",
    "        return '-'.join(ll)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = s.replace('-','')[::-1]\n",
    "        ans,l = [],len(s)\n",
    "        for i in range(0,l,k):\n",
    "            ans.insert(0,s[i:i+k][::-1].upper())\n",
    "        return '-'.join(ans)        \n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s=s.upper().replace(\"-\",\"\")\n",
    "        if len(s)==1:\n",
    "            return s\n",
    "        b,c=\"\",[]\n",
    "        if len(s)%k!=0:\n",
    "            c.append(s[0:len(s)%k]+\"-\")\n",
    "            s=s[len(s)%k:]\n",
    "        for i,j in enumerate(s):\n",
    "            b+=j\n",
    "            if len(b)==k:\n",
    "                if i==len(s)-1:\n",
    "                    c.append(b)\n",
    "                else:\n",
    "                    c.append(b+\"-\")\n",
    "                    b=\"\"\n",
    "        return \"\".join(c)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        new = \"\".join(s.split(\"-\"))\n",
    "        ret = []\n",
    "        for i in range(len(new),0,-k):\n",
    "            ret.insert(0,new[max(0,i-k):i].upper())\n",
    "        return \"-\".join(ret)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        keys_list = s.split(\"-\")\n",
    "        keys_str = ''.join(keys_list)\n",
    "        new_keys_list = []\n",
    "        for i in range(len(keys_str), 0, -k):\n",
    "            new_keys_list.append(keys_str[max(i-k, 0): i].upper())\n",
    "\n",
    "        new_keys_list.reverse()\n",
    "        return '-'.join(new_keys_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        n = len(s)\n",
    "        tmplen = 0\n",
    "        tmpstr = \"\"\n",
    "        res = []\n",
    "        for i in range(n-1, -1, -1):\n",
    "            if s[i] == '-':\n",
    "                continue\n",
    "            # 下面这样加，就不用处理顺序了\n",
    "            tmpstr = s[i] + tmpstr\n",
    "            tmplen += 1\n",
    "            if tmplen == k:\n",
    "                res.append(tmpstr.upper())\n",
    "                tmplen = 0\n",
    "                tmpstr = \"\"\n",
    "        if tmpstr != \"\":\n",
    "            res.append(tmpstr.upper())\n",
    "        return \"-\".join(res[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        all_char = s.replace(\"-\", \"\")\n",
    "        num_char = len(all_char)\n",
    "\n",
    "        rem = num_char % k\n",
    "        first_len = rem if rem > 0 else k\n",
    "\n",
    "        results = [all_char[:first_len]]\n",
    "        for idx in range(first_len, num_char, k):\n",
    "            results.append(all_char[idx:idx+k])\n",
    "\n",
    "        return \"-\".join([substr.upper() for substr in results])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        res = []\n",
    "        i = 0\n",
    "        tmp = ''\n",
    "        for c in s[::-1]:\n",
    "            if i < k:\n",
    "                if c == '-':\n",
    "                    continue\n",
    "                tmp += c.upper()\n",
    "                i += 1\n",
    "            else:\n",
    "                res.append(tmp[::-1])\n",
    "                tmp = ''\n",
    "                i = 0\n",
    "                if c == '-':\n",
    "                    continue\n",
    "                tmp += c.upper()\n",
    "                i += 1\n",
    "        if tmp:\n",
    "            res.append(tmp[::-1])\n",
    "        print(res)\n",
    "        return '-'.join(res[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        l = []\n",
    "        for ch in s:\n",
    "            if ch.isnumeric():\n",
    "                l.append(ch)\n",
    "            if ch.isalpha():\n",
    "                l.append(ch.upper())\n",
    "        ans = []\n",
    "        count = 0\n",
    "        for ch in reversed(l):\n",
    "            ans.append(ch)\n",
    "            count += 1\n",
    "            if count == k:\n",
    "                ans.append('-')\n",
    "                count = 0\n",
    "        if ans and ans[-1] == '-':\n",
    "            ans.pop()\n",
    "        return ''.join(reversed(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        dash_cnt = s.count(\"-\")\n",
    "        ch_cnt = len(s) - dash_cnt\n",
    "        first = ch_cnt % k\n",
    "        for word in s:\n",
    "            if word != '-':\n",
    "                ans.append(word.upper())\n",
    "                cnt += 1\n",
    "                if cnt % k == first:\n",
    "                    ans.append('-')\n",
    "        if ans and ans[-1] == '-':\n",
    "            ans.pop()\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ans=list()\n",
    "        cnt=0\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            if s[i]!='-':\n",
    "                ans.append(s[i].upper())\n",
    "                cnt+=1\n",
    "                if cnt%k==0:\n",
    "                    ans.append('-')\n",
    "        if ans:\n",
    "            ans.reverse()\n",
    "            if ans[0]=='-':\n",
    "                del ans[0]\n",
    "            return (''.join(ans))\n",
    "        else:\n",
    "            return ''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        i = len(s) - 1\n",
    "        res = []\n",
    "        while i >= 0:\n",
    "            cnt = 0\n",
    "            while cnt < k:\n",
    "                if s[i].isalnum():\n",
    "                    res.append(s[i].upper())\n",
    "                    cnt += 1\n",
    "                i -= 1\n",
    "                if i < 0:\n",
    "                    break\n",
    "            res.append('-')\n",
    "        res.reverse()\n",
    "        return \"\".join(res).strip('-')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s=s.upper()\n",
    "        s=list(s)\n",
    "        s.reverse()\n",
    "        tp=[]\n",
    "        c=0\n",
    "        ls=[]\n",
    "        find=False\n",
    "        for ch in s:\n",
    "            if ch=='-':\n",
    "                continue\n",
    "            find=True\n",
    "            c+=1\n",
    "            tp.append(ch)\n",
    "            if c==k:\n",
    "                tp.reverse()\n",
    "                c=0\n",
    "                ls.append('-'+\"\".join(tp))\n",
    "                tp=[]\n",
    "        if c!=0:\n",
    "            tp.reverse()\n",
    "            ls.append('-'+\"\".join(tp))\n",
    "        if not find:\n",
    "            return \"\"\n",
    "        ls.reverse()\n",
    "        return \"\".join(ls)[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s_list =  list(\"\".join(s.upper().split(\"-\")))\n",
    "        n = len(s_list)\n",
    "        first, group = n % k, n//k\n",
    "        s1 = \"\".join(s_list[0:first])\n",
    "        s2_list = s_list[first:]\n",
    "        s3_list = [\"-\" + \"\".join(s2_list[i:i + k]) for i in range(0, len(s2_list), k)]\n",
    "        s2 = \"\".join(s3_list)\n",
    "        s = s1 + s2\n",
    "        if len(s) > 0 and s[0] == \"-\":\n",
    "            return s[1:]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        s = ''.join(s.upper().split('-'))\n",
    "        p = len(s)%k\n",
    "        if p == 0: p+=k\n",
    "        while p < len(s):\n",
    "            s = s[:p] + '-' + s[p:]\n",
    "            p+= k+1\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ans = list()\n",
    "        cnt = 0\n",
    "\n",
    "        for i in range(len(s) - 1, -1, -1):\n",
    "            if s[i] != \"-\":\n",
    "                ans.append(s[i].upper())\n",
    "                cnt += 1\n",
    "                if cnt % k == 0:\n",
    "                    ans.append(\"-\")\n",
    "        \n",
    "        if ans and ans[-1] == \"-\":\n",
    "            ans.pop()\n",
    "        \n",
    "        return \"\".join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        rs = []\n",
    "        count = 0\n",
    "        s = s.replace('-','')\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            rs.append(s[i].upper())\n",
    "            count += 1\n",
    "            if count==k and i!=0:\n",
    "                rs.append('-')\n",
    "                count = 0\n",
    "        return \"\".join(rs[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ans = list()\n",
    "        cnt = 0\n",
    "\n",
    "        for i in range(len(s) - 1, -1, -1):\n",
    "            if s[i] != \"-\":\n",
    "                ans.append(s[i].upper())\n",
    "                cnt += 1\n",
    "                if cnt % k == 0:\n",
    "                    ans.append(\"-\")\n",
    "        \n",
    "        if ans and ans[-1] == \"-\":\n",
    "            ans.pop()\n",
    "        \n",
    "        return \"\".join(ans[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ans = []\n",
    "        count = 0\n",
    "\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            if s[i] !='-':\n",
    "                ans.append(s[i].upper())\n",
    "                count += 1\n",
    "                if count % k == 0:\n",
    "                    ans.append('-')\n",
    "        \n",
    "        if ans and ans[-1] == '-':\n",
    "            ans.pop()\n",
    "\n",
    "        return \"\".join(ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def licenseKeyFormatting(self, s: str, k: int) -> str:\n",
    "        ans = list()\n",
    "        cnt = 0\n",
    "\n",
    "        for i in range(len(s) - 1, -1, -1):\n",
    "            if s[i] != \"-\":\n",
    "                ans.append(s[i].upper())\n",
    "                cnt += 1\n",
    "                if cnt % k == 0:\n",
    "                    ans.append(\"-\")\n",
    "        \n",
    "        if ans and ans[-1] == \"-\":\n",
    "            ans.pop()\n",
    "        \n",
    "        return \"\".join(ans[::-1])\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
